
function DateUtil(){
	
}

//格式化日期：yyyy-MM-dd hh:mm:ss
Date.prototype.format = function(format){
	var o =
    {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    
    if(/(y+)/.test(format)){
    	format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    
    for(var k in o){
    	if(new RegExp("("+ k +")").test(format)){
    		format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    	}
    }

    return format;
}

//获取前天日期
DateUtil.getBeforeDay = function(format){
	var d = new Date();
	d = new Date(d.getYear(),d.getMonth(),d.getDate());
	d.setDate(d.getDate()-1) ;
	if(format!=undefined) return d.format(format);
	return d;  
}

//获取后天日期
DateUtil.getAfterDay = function(format){
	var d = new Date();
	d.setDate(d.getDate()+1) ;
	if(format!=undefined) d = d.format(format);
	return d; 
}

//获取当月的最后一天
DateUtil.getLastDay = function(format){
	var d = new Date();
	d.setMonth(d.getMonth()+1)   
  	d.setDate(0);   
  	if(format!=undefined) d = d.format(format);
	return d; 
}

//获取指定日期的相隔日期
DateUtil.dateAdd = function(sInterval,   iNumber,   dDate){   
	var dTemp   =   new   Date(dDate);  
	if   (dTemp   ==   "NaN")   dTemp   =   new   Date();   
	switch   (sInterval)   {   
		case   "s"   :   
			return   new   Date(Date.parse(dTemp)   +   (1000   *   iNumber));   
		case   "n"   :   
			return   new   Date(Date.parse(dTemp)   +   (60000   *   iNumber));   
		case   "h"   :	//时
			return   new   Date(Date.parse(dTemp)   +   (3600000   *   iNumber));   
		case   "d"   :	//天
			return   new   Date(Date.parse(dTemp)   +   (86400000   *   iNumber));   
		case   "w"   :	//周
			return   new   Date(Date.parse(dTemp)   +   ((86400000   *   7)   *   iNumber));   
		case   "m"   :	//月
			return   new   Date(dTemp.getFullYear(),   (dTemp.getMonth())   +   iNumber,   dTemp.getDate());   
		case   "y"   :	//年
			return   new   Date((dTemp.getFullYear()   +   iNumber),   dTemp.getMonth(),   dTemp.getDate());   
	}   
}   


